package com.github.baichuan.application.scaffold.rest.jpa.identity;

import com.github.baichuan.application.scaffold.rest.domain.identity.ApplicationMember;
import com.github.baichuan.application.scaffold.rest.domain.identity.MemberLogin;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import lombok.NonNull;
import org.springframework.data.jpa.domain.Specification;

public class MemberLoginSpecification implements Specification<MemberLogin> {
    private ApplicationMember applicationMember;

    public MemberLoginSpecification(@NonNull ApplicationMember applicationMember){
        this.applicationMember = applicationMember;
    }

    @Override
    public Predicate toPredicate(Root<MemberLogin> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
        Predicate restrictions = builder.equal(root.get("application"), (Object)applicationMember.getApplication());
        restrictions = builder.and(restrictions, builder.equal(root.get("member"), applicationMember.getMember()));
        return restrictions;
    }
}
